import React, { Component } from 'react'
import { Route, Redirect, Switch } from 'react-router-dom'

export default class Index extends Component {
    render() {
        const { routerList } = this.props
        // 筛选不需要重定向的数据
        const filterRoutes = routerList.filter(item => !item.to)
        // 筛选需要重定向的数据
        const redirectRoutes = routerList.filter(item => item.to)
        return (
            <Switch>
                {
                    filterRoutes.map((item, index) => {
                        return <Route path={item.path} render={(prop) => {
                            let Com = item.com
                            if (item.auth && !sessionStorage.getItem('token')) {
                                // return <Redirect to = "/login"/>
                                return <Redirect to = {{
                                    pathname: "/login",
                                    state: {
                                        back: item.path
                                    }
                                }}/>
                            } else {
                                if (item.children) {
                                    return <Com child={item.children} />
                                } else {
                                    return <Com/>
                                }
                            }
                        }} key={index} />
                    })
                }

                {
                    redirectRoutes.map((item, index) => {
                        return <Redirect to={item.to} from={item.from} key={index} />
                    })
                }
            </Switch>
        )
    }
}
